# -*- coding: <encoding name> -*-
"""
@Author：mengying
@file： myAuthorization.py
@date：2023/6/14 16:46
@email: 652044581@qq.com
@desc: 授权相关
"""
from utils.myCache import Cache

# 配置缓存
RedisClient = Cache().select('redis').current_db


class CacheKeys:
    TOKEN_NAME = "admin_token"


class Authorization:

    @staticmethod
    def get_token(request):
        return request.headers.get(CacheKeys.TOKEN_NAME, '')

    @staticmethod
    async def get_user_info(request):

        token = request.headers.get(CacheKeys.TOKEN_NAME, '')

        # 判断token是否过期
        if RedisClient.__module__ == "redis.client":
            user = RedisClient.get(token)
        else:
            user = await RedisClient.get(token)

        # token续期
        RedisClient.expire(token, 30 * 60)

        setattr(request.headers, "user_info", user)

    @staticmethod
    def white_list_check(request):
        white_list = [
            '/api/demo/sync',
            '/api/demo/async',
            '/api/demo/health',
        ]
        if request.path in white_list:
            return True
        else:
            return False
